//
//  ShellWallertManager.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/8.
//  Copyright © 2019年 VV. All rights reserved.
//

#import "ShellWallertManager.h"

@implementation ShellWallertManager
//单例
+ (instancetype)sharedInstance{
    static dispatch_once_t onceToken;
    static ShellWallertManager * manager ;
    dispatch_once(&onceToken, ^{
        manager = [[ShellWallertManager alloc]init];
    });
    return manager;
}

/*!
 iOS判断升级
 @param isTip           是否弹出提示
 @discussion 检查是否是最新版本。
 */
- (void)checkVersion:(BOOL)isTip{
    
    //此获取的版本号对应version，打印出来对应为1.2.3.4.5这样的字符串
    //本地版本号
    NSString *localVerson = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
    NSString *url = [NSString stringWithFormat:@"%@/%@",kAppUpdates,@"ios"];
    [LZNetworkingManager lz_request:@"get" url:url params:nil success:^(id  _Nullable responseObject) {
        if([responseObject[kResultStatus] isEqualToString:kCode]){
            NSDictionary *dataDict = responseObject[kResultData];
            NSString *currentVersion = dataDict[@"version"];
            currentVersion = [currentVersion substringFromIndex:1];
            NSString *downloadUrl = [NSString stringWithFormat:@"%@",dataDict[@"url"]];
            if(![downloadUrl hasPrefix:@"itms-services://?action=download-manifest&url="]){
                downloadUrl = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=%@",downloadUrl];
            }
            downloadUrl = [downloadUrl stringByReplacingOccurrencesOfString:@" " withString:@""];
            NSString *remark = [NSString stringWithFormat:@"%@",dataDict[@"remark"]];
            if ([currentVersion compare:localVerson options:NSNumericSearch] != NSOrderedDescending){
                //不弹出提示更新弹框
                return ;
            }
            remark = [remark stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
            remark = [remark stringByReplacingOccurrencesOfString:@"\\r" withString:@"\r"];

            [LZToolView showAlertType:LZAlertViewTypeSystemAlter withTitle:LZLocalizedString(@"发现新版本") message:remark cancel:nil sure:LZLocalizedString(@"确认") objectDict:nil cancelBlock:^(id  _Nullable object) {} sureBlock:^(id  _Nullable object) {
              
                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downloadUrl]];
                            
                exit(0);
                
              }];
              
            
        }

    } failure:^(NSError * _Nullable error) {
        
    }];
    
    
//
//    [LZHttpTool cd_getBaseUrl:kGetNewest params:param success:^(NSDictionary *responseObject) {
//        if([responseObject[kResultStatus] integerValue] == kResultCode){
//            NSDictionary *dataDict = responseObject[kResultData];
//            NSString *currentVersion = [NSString stringWithFormat:@"%@",dataDict[@"version"]];
//            NSString *content = [NSString stringWithFormat:@"%@", dataDict[@"content"]];
//            NSString *downloadUrl = [NSString stringWithFormat:@"%@",dataDict[@"apkUrl"]];
//            if ([currentVersion compare:localVerson options:NSNumericSearch] != NSOrderedDescending){
//                //不弹出提示更新弹框
//                return ;
//            }
//            content = [content stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
//            [LZToolView showAlertType:LZAlertViewTypeCustomerAlerView
//                            withTitle:LZLocalizedString(@"發現新版本")
//                              message:NullClass(content)
//                               cancel:nil
//                                 sure:LZLocalizedString(@"立即更新")
//                           objectDict:@{@"textAlignment":@(NSTextAlignmentLeft),
//                                        @"version":@(1)}
//                          cancelBlock:nil
//                            sureBlock:^(id object) {
//                                [[UIApplication sharedApplication] openURL:[NSURL URLWithString:downloadUrl]];
//                                exit(0);
//                            }];
//        }
//
//    }
//                      failure:^(NSError *error) {
//
//                      }];
//
}
@end
